# Last Change: Sat May 03 02:00 PM 2008 J
# vim:syntax=python

import os
from os.path import join as pjoin, splitext

from numscons import GetNumpyEnvironment
from numscons import CheckF77LAPACK,\
                                  CheckCLAPACK, \
                                  IsATLAS, GetATLASVersion, \
                                  CheckF77Clib
from numscons import write_info

from scons_support import do_generate_fake_interface, \
                          generate_interface_emitter

env = GetNumpyEnvironment(ARGUMENTS)
env.Tool('f2py')
#if os.name == 'nt':
#    # NT needs the pythonlib to run any code importing Python.h, including
#    # simple code using only typedef and so on, so we need it for configuration
#    # checks
#    env.AppendUnique(LIBPATH = [get_pythonlib_dir()])

#=======================
# Starting Configuration
#=======================
config = env.NumpyConfigure(custom_tests = {'CheckCLAPACK' : CheckCLAPACK,
                                            'CheckLAPACK' : CheckF77LAPACK,
                                            'CheckF77Clib' : CheckF77Clib})

#--------------
# Checking Blas
#--------------
st = config.CheckLAPACK(check_version = 1)
if not st:
    raise RuntimeError("no lapack found, necessary for lapack module")

if IsATLAS(env, 'lapack'):
    version = GetATLASVersion(env)
    env.Append(CPPDEFINES = [('ATLAS_INFO', '"\\"%s"\\"' % version)])
else:
    env.Append(CPPDEFINES = [('NO_ATLAS_INFO', 1)])

if config.CheckCLAPACK():
    has_clapack = 1
else:
    has_clapack = 0

config.Finish()
write_info(env)

#==========
#  Build
#==========
env.AppendUnique(F2PYOPTIONS = '--quiet')

env['BUILDERS']['GenerateFakePyf'] = Builder(action = do_generate_fake_interface,
                                  emitter = generate_interface_emitter)

#------------
#   flapack
#------------
yop = env.FromFTemplate('flapack.pyf', 'flapack.pyf.src')
env.NumpyPythonExtension('flapack', source = ['flapack.pyf'])

#------------
#   clapack
#------------
if has_clapack:
    env.FromFTemplate('clapack.pyf', 'clapack.pyf.src')
else:
    env.GenerateFakePyf('clapack', 'clapack.pyf.src')
env.NumpyPythonExtension('clapack', source = 'clapack.pyf')

#----------------
# calc_lwork:
#----------------
calc_src = env.F2py(pjoin('calc_lworkmodule.c'), 
                         source = pjoin('calc_lwork.f'))
env.NumpyPythonExtension('calc_lwork', source = calc_src + ['calc_lwork.f'])

#--------------
# Atlas version
#--------------
env.NumpyPythonExtension('atlas_version', 'atlas_version.c')
